//
//  WJMeCell.m
//  WJBSApp
//
//  Created by dllo on 16/7/16.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJMeCell.h"
#import "WJMeModel.h"
#import "WJEssAndNewLable.h" // 字体是灰色的 Lable
#import "WJTieZiLable.h"


@interface WJMeCell ()

@property (nonatomic,retain) WJBaseImageView *image_listImageView;

@property (nonatomic,retain) WJBaseLable *theme_nameLable;

@property (nonatomic,retain) WJEssAndNewLable *sub_numberLable; //订阅

@property (nonatomic,retain) WJTieZiLable *post_numLable; //帖数

@property (nonatomic,retain) UILabel *belowLable;

@end


@implementation WJMeCell

-(void)dealloc
{
    [_model release];
    [_image_listImageView release];
    [_theme_nameLable release];
    [_sub_numberLable release];
    [_dyButton release];
    [_post_numLable release];
    [_belowLable release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.image_listImageView = [[WJBaseImageView alloc]init];
        [self.contentView addSubview:self.image_listImageView];
        [_image_listImageView release];
        
        self.theme_nameLable = [[WJBaseLable alloc]init];
        [self.contentView addSubview:self.theme_nameLable];
        [_theme_nameLable release];
        
        self.sub_numberLable = [[WJEssAndNewLable alloc]init];
        [self.contentView addSubview:self.sub_numberLable];
        [_sub_numberLable release];
        
        self.post_numLable = [[WJTieZiLable alloc]init];
        [self.contentView addSubview:self.post_numLable];
        self.post_numLable.textColor = [UIColor grayColor];
        [_post_numLable release];
        
        self.dyButton = [WJBaseButton buttonWithType:UIButtonTypeSystem];
        [self.contentView addSubview:self.dyButton];
        [_dyButton setTitle:@"+订阅" forState:UIControlStateNormal];
        _dyButton.titleLabel.font = [UIFont systemFontOfSize:15];
        
        self.belowLable = [[UILabel alloc]init];
        [self.contentView addSubview:self.belowLable];
        self.belowLable.backgroundColor = [UIColor grayColor];
        [_belowLable release];
        
        
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.image_listImageView.frame = CGRectMake(10, 10, HEIGHT / 9 - 20,HEIGHT / 9 - 20);
    
    self.theme_nameLable.frame = CGRectMake(15 +  HEIGHT / 9 - 20, 10, 200, (HEIGHT / 9 - 20) / 2);
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
    CGRect rect = [self.sub_numberLable.text boundingRectWithSize:CGSizeMake(1000, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.sub_numberLable.frame = CGRectMake(15 +  HEIGHT / 9 - 20, HEIGHT / 18, rect.size.width + 20, (HEIGHT / 9- 20) / 2 - 1);
    self.sub_numberLable.font = [UIFont systemFontOfSize:14];
    
    CGRect rect1 = [self.post_numLable.text boundingRectWithSize:CGSizeMake(1000, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.post_numLable.frame = CGRectMake(15 +  HEIGHT / 9 - 20 +  rect.size.width + 20, HEIGHT / 18, rect1.size.width + 20 , (HEIGHT / 9- 20) / 2 - 1);
    self.post_numLable.font = [UIFont systemFontOfSize:14];
     
    self.dyButton.frame = CGRectMake(WIDTH * 6 / 7 - 10, (HEIGHT / 9 - 30) / 2, WIDTH / 7, 30);
    self.belowLable.frame = CGRectMake(0, HEIGHT/9 - 1, WIDTH , 1);
    
}

- (void)setModel:(WJMeModel *)model
{
    if (_model !=model) {
        [_model release];
        _model = [model retain];
    }
    
    [self.image_listImageView sd_setImageWithURL:[NSURL URLWithString:model.image_list]placeholderImage:[UIImage imageNamed:@"defaultTagIcon~iphone"]];
    
    self.theme_nameLable.text = model.theme_name;
    
    // 大于1万和小于1万显示不同
    
    if (model.sub_number >= 10000) {
        NSString *str = [NSString stringWithFormat:@"%.1f万人订阅",model.sub_number/10000.0];
        self.sub_numberLable.text = [str stringByReplacingOccurrencesOfString:@".0" withString:@""];
    }else{
        self.sub_numberLable.text = [NSString stringWithFormat:@"%ld人订阅",(long)model.sub_number];
    }
    
    // 将右边的字变成红色
    
    NSString *string = [NSString stringWithFormat:@"总帖数%ld",(long)model.post_num];
    NSString *str = [NSString stringWithFormat:@"%ld",(long)model.post_num];
    NSMutableAttributedString *attriBute = [[NSMutableAttributedString alloc]initWithString:string];
    NSRange range  = [string rangeOfString:str];
    NSDictionary *dicA = @{NSForegroundColorAttributeName:[UIColor redColor]};
    [attriBute setAttributes:dicA range:range];
    
    self.post_numLable.attributedText = attriBute;

    
}








@end
